﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Windows.Forms;
using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Renderers.Core;

namespace NetJsWire.Widgets.Renderers.Tree
{
    [ ClassName( "Ext.tree.TreePanel" ) ]
    internal class TreePanel : Widget
    {
        private readonly Ui.Tree.TreePanel _tp;

        protected string _CLICK =
            "{0}.on(\"click\", function(node, e) {{ SendToServer(\"{0}\", \"Click\", \"{1}\", node.id); }}, {0});";

        protected string _NODEDROP = "";
        private string _rootNode = string.Empty;

        public TreePanel( object baseObject )
            : base( baseObject )
        {
            _tp = ( Ui.Tree.TreePanel ) baseObject;
        }

        public override void Render()
        {
            if ( !Rendered )
            {
                EnsureParentNodePosition();

                SetConstructionProperty( "id", InternalID );
                SetAttribute( ControlNode, "parent", ClientParentToRender );
                SetStyleProperty( "position", "absolute" );
                //AddStyleProperty("top", ((Control)Base).Top);
                //AddStyleProperty("left", ((Control)Base).Left);
                SetStyleProperty( "width", ( ( Control ) Base ).Width );
                SetStyleProperty( "height", ( ( Control ) Base ).Height );
                //AddStyleProperty("height", "'100%'");
                SetConstructionProperty( "height", ( ( Control ) Base ).Height );
                //SetConstructionProperty("height", "'100%'");
                if ( !_tp.ShowRoot )
                    SetConstructionProperty( "rootVisible", false );
                if ( !_tp.ShowLines )
                    SetConstructionProperty( "lines", false );
                if ( _tp.AllowDrop )
                    SetConstructionProperty( "enableDD", true );
                ControlNode.AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element", "nodes", "" ) );
                foreach ( TreeNode tn in _tp.Nodes )
                {
                    var node =
                        ControlNode[ "nodes" ].AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element",
                                                                                                            "node", "" ) );
                    node.Attributes.Append( WebApplication.Instance.XmlDocument.CreateAttribute( "name" ) ).Value =
                        tn.Name;
                    node.Attributes.Append( WebApplication.Instance.XmlDocument.CreateAttribute( "text" ) ).Value =
                        tn.Text;
                    if ( tn.Level == 0 )
                    {
                        //is the root node
                        node.Attributes.Append( WebApplication.Instance.XmlDocument.CreateAttribute( "root" ) ).Value =
                            "true";
                    }
                    else if ( tn.Parent != null && tn.Parent.Level != 0 )
                    {
                        SetAttribute( node, "parent", tn.Parent.Name );
                    }
                    if ( !string.IsNullOrEmpty( tn.ToolTipText ) )
                    {
                        if ( !tn.ToolTipText.Contains( "{" ) )
                            SetAttribute( node, "qtip", tn.ToolTipText );
                        else
                            SetAttribute( node, "qtipCfg", tn.ToolTipText );
                    }
                    BuildNode( tn );
                }
                //if (_tp.AllowDrop)
                //    sb.AppendFormat("{0}.on(\"nodedrop\", function(dE) {{ SendToServer(\"{0}\", \"NodeDropped\", \"{1}\", [dE.source.id, dE.dropNode.id]) }}, {0});", ClientID, InternalID);
            }

            base.Render();
        }

        private void BuildNode( TreeNode node )
        {
            foreach ( TreeNode tn in node.Nodes )
            {
                var xnode =
                    ControlNode[ "nodes" ].AppendChild( WebApplication.Instance.XmlDocument.CreateNode( "element",
                                                                                                        "node", "" ) );
                xnode.Attributes.Append( WebApplication.Instance.XmlDocument.CreateAttribute( "name" ) ).Value = tn.Name;
                xnode.Attributes.Append( WebApplication.Instance.XmlDocument.CreateAttribute( "text" ) ).Value = tn.Text;

                if ( tn.Nodes.Count > 0 )
                    SetAttribute( xnode, "leaf", false );
                else
                    SetAttribute( xnode, "leaf", true );
                //if (!string.IsNullOrEmpty(tn.ImageKey))
                //    SetAttribute(xnode, "icon", "webapp/Images.ashx?_id=" ); //TODO: need a reference to node... :S
                if ( !string.IsNullOrEmpty( tn.ToolTipText ) )
                {
                    if ( !tn.ToolTipText.Contains( "{" ) )
                        SetAttribute( xnode, "qtip", tn.ToolTipText );
                    else
                        SetAttribute( xnode, "qtipCfg", tn.ToolTipText );
                }
                if ( tn.Parent != null && tn.Parent.Level != 0 )
                {
                    SetAttribute( xnode, "parent", tn.Parent.Name );
                }

                if ( tn.Nodes.Count > 0 )
                    BuildNode( tn );
            }
        }
    }
}